package be.jabapage.util;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.GAEApplicationServlet;

public class SpringVaadinServlet extends GAEApplicationServlet{

	/** Class serial version unique identifier. */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("rawtypes")
	private Class clazz;
	
	private Application application;

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring-config.xml");

		application = context.getBean(Application.class);
		clazz = application.getClass();
	}

	/**
	 * Gets the application from the Spring context.
	 * 
	 * @return The Spring bean named 'application'
	 */
	@Override
	protected Application getNewApplication(HttpServletRequest request)
			throws ServletException {
		return application;
	}

	/**
	 * @see com.vaadin.terminal.gwt.server.AbstractApplicationServlet#getApplicationClass()
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	protected Class getApplicationClass() throws ClassNotFoundException {
		return clazz;
	}
}
